// 全局共享数据示例
import { DEFAULT_TOKEN } from '@/constants';
import { useState } from 'react';
type UserInfo={
	token:string;
	username:string;
	avatar?:string;
	role?:string;
};
const useUser = () => {
	const [token, setToken] = useState<string>(DEFAULT_TOKEN);
	const [username, setUsername] = useState<string>("");
	const [role, setRole] = useState<string>("");
	const [avatar, setAvatar] = useState<string>("");
	const [isLogin, setIsLogin] = useState<boolean>(DEFAULT_TOKEN ? true : false);
	return {
		token, setToken,
		username, setUsername,
		avatar, setAvatar,
		isLogin, setIsLogin,
		role, setRole,
		setUserInfo(userInfo:API.UserInfo|undefined){
			if(userInfo){
				if(userInfo.token){
					setToken(userInfo.token);
					setIsLogin(true)
				}
	
				if(userInfo.username){
					setUsername(userInfo.username||"");
				}
	
				if(userInfo.avatar){
					setAvatar(userInfo.avatar);
				}
	
				if(userInfo.role){
					setRole(userInfo.role||"");
				}
			}
		}
	};
};

export default useUser;
